package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.domain.User;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    /**
     * 获取登录用户名
     *
     * @return
     */
    @RequestMapping("/getUsername")
    public Result getUsername() {
        try {
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User)
                    SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }


    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @RequestMapping("/add")
    public Result insertUser(@RequestBody User user, Integer[] roleIds) {
        try {
            userService.insertUser(user, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            //提示错误信息
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        //提示成功消息
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }


    /**
     * 查询单项用户
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {

        User user = userService.findUserById(id);

        if (user == null) {
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        } else {
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, user);
        }
    }

    /**
     * 分页查询 条件查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult findUserByPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = userService.findUserByPage(queryPageBean);
        return pageResult;
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result deleteUser(Integer id) {
        try {
            userService.deleteUser(id);
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        //删除成功
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 修改用户
     *
     * @return
     */
    @RequestMapping("/update")
    public Result updateUser(@RequestBody User user, Integer[] roleIds) {
        try {
            userService.updateUser(user, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            //编辑 修改失败
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        //编辑成功
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }


    /**
     * 数据回显 用户的角色信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/findRoleIds")
    public List<Integer> findRoleIds(Integer id) {
        return userService.findRoleIds(id);
    }
}
